package com.sxfq.loansupermarket.backsystem.modular.doctorLooked.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.dao.DoctorLookedDao;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.service.IDoctorLookedService;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.PaymentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
@Transactional
public class DoctorLookedServiceImpl implements IDoctorLookedService {

    @Autowired
    private DoctorLookedDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<DoctorLookedInfo> selectPageListByQueryGetMap(Page<DoctorLookedInfo> page, SearchDoctorLookedInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getArrivalTime())) {
            params.put("arrivalTime", searchInfo.getArrivalTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerSex())) {
            params.put("customerSex", searchInfo.getCustomerSex());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerAgeStart())) {
            params.put("customerAgeStart", searchInfo.getCustomerAgeStart());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerAgeEnd())) {
            params.put("customerAgeEnd", searchInfo.getCustomerAgeEnd());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPayServiceName())) {
            params.put("payServiceName", searchInfo.getPayServiceName());
        }
        String sql = "select * from user a left join role b on a.roleId=b.id where a.account='" + ShiroKit.getUser().getAccount() + "'";
        User user = sqlMapper.selectOne(sql, User.class);
        if (user != null) {
            if ("1".equals(user.getRoleid())||"1,".equals(user.getRoleid().substring(0,2))) {
                params.put("doctor", "");
            } else {
                params.put("doctor", ShiroKit.getUser().getAccount());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStatus())) {
            params.put("arrivalStatus", searchInfo.getArrivalStatus());
        }
        /*String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if ("1".equals(userDto.getRoleid())) {
            params.put("doctor", "");
            if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
                params.put("doctor", searchInfo.getDoctor());
            }
        } else {
            params.put("doctor", ShiroKit.getUser().getAccount());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsReturnVisit())) {
            params.put("isReturnVisit", searchInfo.getIsReturnVisit());
        }*/
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "deal_info";
        params.put("tableName1", this.getTableName());
        params.put("tableName", tableName);
        return dao.getQueryPage(page, params);
    }

    @Override
    public List<DoctorLookedInfo> selectChargeSchedule(Page<DoctorLookedInfo> page, SearchDoctorLookedInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPayServiceName())) {
            params.put("payServiceName", searchInfo.getPayServiceName());
        }
        String sql = "select * from user a left join role b on a.roleId=b.id where a.account='" + ShiroKit.getUser().getAccount() + "'";
        User user = sqlMapper.selectOne(sql, User.class);
        if (user != null) {
            if ("1".equals(user.getRoleid())||"1,".equals(user.getRoleid().substring(0,2))) {
                params.put("doctor", "");
            } else {
                params.put("doctor", ShiroKit.getUser().getAccount());
            }
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        params.put("tableName1", tableName1);
        params.put("tableName", this.getTableName());
        return dao.getQueryPageChargeSchedule(page, params);
    }

   //医生划价预览
    public List<DoctorLookedInfo> selectDoctorChargePrice(Page<DoctorLookedInfo> page, SearchDoctorLookedInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCheckStatus())) {
            params.put("checkStatus", searchInfo.getCheckStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
//        String sql = "select * from user a left join role b on a.roleId=b.id where a.account='" + ShiroKit.getUser().getAccount() + "'";
//        User user = sqlMapper.selectOne(sql, User.class);
//        if (user != null) {
//            if ("1".equals(user.getRoleid())||"1,".equals(user.getRoleid().substring(0,2))) {
//                params.put("doctor", "");
//            } else {
//                params.put("doctor", ShiroKit.getUser().getAccount());
//            }
//        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        params.put("tableName1", tableName1);
        params.put("tableName", this.getTableName());
        return dao.getQueryPageDoctorChargePrice(page, params);
    }

    @Override
    public List<DoctorLookedInfo> chargeScheduleExportt(SearchDoctorLookedInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPayServiceName())) {
            params.put("payServiceName", searchInfo.getPayServiceName());
        }
        String sql = "select * from user a left join role b on a.roleId=b.id where a.account='" + ShiroKit.getUser().getAccount() + "'";
        User user = sqlMapper.selectOne(sql, User.class);
        if (user != null) {
            if ("1".equals(user.getRoleid())||"1,".equals(user.getRoleid().substring(0,2))) {
                params.put("doctor", "");
            } else {
                params.put("doctor", ShiroKit.getUser().getAccount());
            }
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        params.put("tableName1", tableName1);
        params.put("tableName", this.getTableName());
        return dao.getQueryPageChargeScheduleExportt(params);
    }

    @Override
    public List<DoctorLookedInfo> doctorChargePriceExport(SearchDoctorLookedInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCheckStatus())) {
            params.put("checkStatus", searchInfo.getCheckStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        params.put("tableName1", tableName1);
        params.put("tableName", this.getTableName());
        return dao.getQueryPageDoctorChargePriceExport(params);
    }


    @Override
    public List<DoctorLookedInfo> selectPageListByDoctorRevisit(Page<DoctorLookedInfo> page, SearchDoctorLookedInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisitTime())) {
            params.put("revisitTime", searchInfo.getRevisitTime());
        }
        params.put("tableName", this.getTableName());
        return dao.getQueryPages(page, params);
    }

    public DoctorLookedInfo selectOneById(String id) {
        String sql = "select * from reservations_info where id= '" + id + "'";
        DoctorLookedInfo info = sqlMapper.selectOne(sql, DoctorLookedInfo.class);
        return info;
    }

    @Override
    public int doctorComment(ReservationsInfo info) {
        String sql = "update reservations_info set jiedai_content='" + info.getJiedaiContent() + "',comment_status='已点评 ' where id='" + info.getId() + "'";
        return sqlMapper.update(sql);
    }

    /**
     * 插入病历数据
     *
     * @param info
     * @return
     */
    @Override
    public int insert(CaseInfo info) {
        String id = CommUtils.getUUID();
        Random rand = new Random();
        int randomNumber = rand.nextInt(999999);
        id = id + randomNumber;
        String finnallyTime = "";
        if (info.getFinnallyTime() == null || info.getFinnallyTime() == "") {
            finnallyTime = "null";
        } else {
            finnallyTime = "'" + info.getFinnallyTime() + "'";
        }
        String sql = "insert into  `case_info` (`id`,`customer_id`,`reservations_id`,`zhushu`,`doctor_id`,`nurse_id`,`type`,`now_medical_history`,`before_medical_history`,`finnally_time`,`doctor_advice`,`lable`,remarkss" +
                ",mouth_check ,auxiliary_check ,diagnose ,remedy_plan ,dispose ,pay_service_id ) VALUES" +
                "('" + id + "','" + info.getCustomerId() + "','" + info.getReservationsId() + "','" + info.getZhushu() + "','" + info.getDoctorId() + "','" + info.getNurseId() + "','" + info.getType() + "','" + info.getNowMedicalHistory() + "','" + info.getBeforeMedicalHistory() + "'" +
                "," + finnallyTime + ",'" + info.getDoctorAdvice() + "' ,'" + info.getLable() + "','" + info.getRemarks() + "','1','2','3','4','5'" +
                ",'" + info.getPayServiceId() + "');";
        sqlMapper.insert(sql);
        String case_id = id; 

        //第一组检查循环插入
        String[] leftUp1 = info.getLeftUp1().split("&&", -1);//
        String[] rightUp1 = info.getRightUp1().split("&&", -1);
        String[] leftDown1 = info.getLeftDown1().split("&&", -1);
        String[] rightDown1 = info.getRightDown1().split("&&", -1);
        String[] remarks1 = info.getRemarks1().split("&&", -1);
        for (int i = 0; i < leftUp1.length; i++) {
            String sql1 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','1','" + leftUp1[i] + "','" + rightUp1[i] + "','" + leftDown1[i] + "','" + rightDown1[i] + "','" + remarks1[i] + "')";
            sqlMapper.insert(sql1);
        }

        //第二组检查循环插入
        String[] leftUp2 = info.getLeftUp2().split("&&", -1);
        String[] rightUp2 = info.getRightUp2().split("&&", -1);
        String[] leftDown2 = info.getLeftDown2().split("&&", -1);
        String[] rightDown2 = info.getRightDown2().split("&&", -1);
        String[] remarks2 = info.getRemarks2().split("&&", -1);
        for (int i = 0; i < leftUp2.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','2','" + leftUp2[i] + "','" + rightUp2[i] + "','" + leftDown2[i] + "','" + rightDown2[i] + "','" + remarks2[i] + "')";
            sqlMapper.insert(sql2);
        }

        //第三组检查循环插入
        String[] leftUp3 = info.getLeftUp3().split("&&", -1);
        String[] rightUp3 = info.getRightUp3().split("&&", -1);
        String[] leftDown3 = info.getLeftDown3().split("&&", -1);
        String[] rightDown3 = info.getRightDown3().split("&&", -1);
        String[] remarks3 = info.getRemarks3().split("&&", -1);
        for (int i = 0; i < leftUp3.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','3','" + leftUp3[i] + "','" + rightUp3[i] + "','" + leftDown3[i] + "','" + rightDown3[i] + "','" + remarks3[i] + "')";
            sqlMapper.insert(sql2);
        }

        //第四组检查循环插入
        String[] leftUp4 = info.getLeftUp4().split("&&", -1);
        String[] rightUp4 = info.getRightUp4().split("&&", -1);
        String[] leftDown4 = info.getLeftDown4().split("&&", -1);
        String[] rightDown4 = info.getRightDown4().split("&&", -1);
        String[] remarks4 = info.getRemarks4().split("&&", -1);
        for (int i = 0; i < leftUp4.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','4','" + leftUp4[i] + "','" + rightUp4[i] + "','" + leftDown4[i] + "','" + rightDown4[i] + "','" + remarks4[i] + "')";
            sqlMapper.insert(sql2);
        }
        //第五组检查循环插入
        String[] leftUp5 = info.getLeftUp5().split("&&", -1);
        String[] rightUp5 = info.getRightUp5().split("&&", -1);
        String[] leftDown5 = info.getLeftDown5().split("&&", -1);
        String[] rightDown5 = info.getRightDown5().split("&&", -1);
        String[] remarks5 = info.getRemarks5().split("&&", -1);
        for (int i = 0; i < leftUp5.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','5','" + leftUp5[i] + "','" + rightUp5[i] + "','" + leftDown5[i] + "','" + rightDown5[i] + "','" + remarks5[i] + "')";
            sqlMapper.insert(sql2);
        }
        String sql1 ="update "+this.getTableName()+" set is_status =1 where id='"+info.getCustomerId()+"'";
        return sqlMapper.update(sql1);
    }


    public int delete(CaseInfo info) {
        return 0;
    }

    public int update(CaseInfo info) {
        //根据传入的病例ID，删除掉病例原有关联的牙位信息
        String sql = "delete from tooth_site where case_id ='" + info.getId() + "'";
        sqlMapper.delete(sql);

        //重新插入牙位表信息
        String case_id = info.getId();

        //第一组检查循环插入
        String[] leftUp1 = info.getLeftUp1().split("&&", -1);
        String[] rightUp1 = info.getRightUp1().split("&&", -1);
        String[] leftDown1 = info.getLeftDown1().split("&&", -1);
        String[] rightDown1 = info.getRightDown1().split("&&", -1);
        String[] remarks1 = info.getRemarks1().split("&&", -1);
        for (int i = 0; i < leftUp1.length; i++) {
            String sql1 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','1','" + leftUp1[i] + "','" + rightUp1[i] + "','" + leftDown1[i] + "','" + rightDown1[i] + "','" + remarks1[i] + "')";
            sqlMapper.insert(sql1);
        }

        //第二组检查循环插入
        String[] leftUp2 = info.getLeftUp2().split("&&", -1);
        String[] rightUp2 = info.getRightUp2().split("&&", -1);
        String[] leftDown2 = info.getLeftDown2().split("&&", -1);
        String[] rightDown2 = info.getRightDown2().split("&&", -1);
        String[] remarks2 = info.getRemarks2().split("&&", -1);
        for (int i = 0; i < leftUp2.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','2','" + leftUp2[i] + "','" + rightUp2[i] + "','" + leftDown2[i] + "','" + rightDown2[i] + "','" + remarks2[i] + "')";
            sqlMapper.insert(sql2);
        }

        //第三组检查循环插入
        String[] leftUp3 = info.getLeftUp3().split("&&", -1);
        String[] rightUp3 = info.getRightUp3().split("&&", -1);
        String[] leftDown3 = info.getLeftDown3().split("&&", -1);
        String[] rightDown3 = info.getRightDown3().split("&&", -1);
        String[] remarks3 = info.getRemarks3().split("&&", -1);
        for (int i = 0; i < leftUp3.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','3','" + leftUp3[i] + "','" + rightUp3[i] + "','" + leftDown3[i] + "','" + rightDown3[i] + "','" + remarks3[i] + "')";
            sqlMapper.insert(sql2);
        }

        //第四组检查循环插入
        String[] leftUp4 = info.getLeftUp4().split("&&", -1);
        String[] rightUp4 = info.getRightUp4().split("&&", -1);
        String[] leftDown4 = info.getLeftDown4().split("&&", -1);
        String[] rightDown4 = info.getRightDown4().split("&&", -1);
        String[] remarks4 = info.getRemarks4().split("&&", -1);
        for (int i = 0; i < leftUp4.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','4','" + leftUp4[i] + "','" + rightUp4[i] + "','" + leftDown4[i] + "','" + rightDown4[i] + "','" + remarks4[i] + "')";
            sqlMapper.insert(sql2);
        }
        //第五组检查循环插入
        String[] leftUp5 = info.getLeftUp5().split("&&", -1);
        String[] rightUp5 = info.getRightUp5().split("&&", -1);
        String[] leftDown5 = info.getLeftDown5().split("&&", -1);
        String[] rightDown5 = info.getRightDown5().split("&&", -1);
        String[] remarks5 = info.getRemarks5().split("&&", -1);
        for (int i = 0; i < leftUp5.length; i++) {
            String sql2 = "insert into tooth_site (case_id,ya_type,left_up,right_up,left_down,right_down,remarks) VALUES" +
                    "('" + case_id + "','5','" + leftUp5[i] + "','" + rightUp5[i] + "','" + leftDown5[i] + "','" + rightDown5[i] + "','" + remarks5[i] + "')";
            sqlMapper.insert(sql2);
        }

        //根据病例ID修改对应的病例信息
        String sql6 = "update case_info a set a.zhushu='" + info.getZhushu().trim() + "',a.now_medical_history='" + info.getNowMedicalHistory().trim() + "'," +
                "a.doctor_id='" + info.getDoctorId().trim() + "',a.nurse_id='" + info.getNurseId().trim() + "'," +
                "a.before_medical_history='" + info.getBeforeMedicalHistory().trim() + "'," +
                "a.doctor_advice='" + info.getDoctorAdvice().trim() + "',a.type='" + info.getType() + "'," +
                "a.remarkss='" + info.getRemarks() + "',a.finnally_time='" + info.getFinnallyTime() + "' where id = '" + info.getId() + "'";
        return sqlMapper.update(sql6);
    }

    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }
}
